<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/11/28/028
 * Time: 17:36
 */

namespace app\api\model;


use app\api\service\Token;
use app\api\service\Shop as ShopService;
use app\lib\exception\ShopException;
use app\api\service\NoteCode as NoteCodeService;
use app\api\model\PatternMaster as PatternMasterModel;
use app\api\model\Config as ConfigModel;

class Shop extends BaseModel {
    protected $hidden = ['update_time','delete_time', 'create_time'];

    /**
     * 关联店铺经营分类
     * @return \think\model\relation\HasMany
     */
    public function shopCategory() {
        return $this->hasMany('ShopCategory','shop_id', 'id');
    }

    /**
     * 关联店铺评价信息
     * @return \think\model\relation\hasMany
     */
    public function comments() {
        return $this->hasMany('Comment', 'shop_id', 'id');
    }

    /**
     * 关联店铺评价信息
     * @return float|int
     */
    public function commentLevel() {
        return $this->hasMany('Comment', 'shop_id', 'id')->avg('service_level');
    }

    /**
     * 关联店铺图片
     * @return \think\model\relation\hasMany
     */
    public function images() {
        return $this->hasMany('ShopImage', 'shop_id', 'id');
    }

    /**
     * 获取器处理店铺logo
     * @param $value
     * @return string
     */
    public function getLogoAttr($value)
    {
        return get_img_url($value);
    }

    public function getStoreImg_1Attr($value) {
        return get_img_url($value);
    }
    public function getStoreImg_2Attr($value) {
        return get_img_url($value);
    }
    public function getStoreImg_3Attr($value) {
        return get_img_url($value);
    }

    /**
     * 获取器处理店铺营业执照图片
     * @param $value
     * @return string
     */
    public function getBusinessLicenseImageAttr($value)
    {
        return get_img_url($value);
    }

    /**
     * 获取器处理店铺经营许可证
     * @param $value
     * @return string
     */
    public function getBusinessCertificateAttr($value)
    {
        return get_img_url($value);
    }

    /**
     * 获取器处理店铺经营许可证
     * @param $value
     * @return string
     */
    public function getCommentLevelCountAttr($value)
    {
        return $value == 0 ? 5 : $value;
    }

    /**
     * 添加或者编辑店铺申请
     * @param $params
     * @return bool
     * @throws ShopException
     */
    public static function saveShop($params){

        $uid = Token::getCurrentUid();

        $shop = self::get(['member_id' => $uid]);
        if ($shop) {
            if ($shop['status'] == 2) {
                throw new ShopException([
                    'msg' => '您已经开通店铺，请勿重复申请',
                    'errorCode' => 16001
                ]);
            }
            $params['status'] = 1; // 更新数据  状态重置为1
            $shop->save($params);
        } else {
            $params['member_id'] = $uid; // 添加数据  绑定用户
            self::create($params);
        }

        // 短信通知
        $part_order_number = ConfigModel::get(['name' => 'part_order_number']);  // 杨静秋
        $manage_mobile = $part_order_number['value'];
        $param = [
            'mobile' => $manage_mobile,
            'name' => $params['name'],
            'p_mobile' => $params['mobile'],
            'type' => 7,
        ];
        NoteCodeService::send_code($param);
        return true;
    }

    /**
     * 获取店铺量体师列表
     * @return array
     */
    public static function shopPatternMaster() {
        $uid = Token::getCurrentUid();
        $shop = self::get(['member_id' => $uid]);
        ShopService::checkShop($shop);

        $pattern_master = PatternMasterModel::all(['relevance_shop_id' => $shop['id']], 'patternMasterMember.memberRoleInfo');
        $data = [];
        foreach ($pattern_master as $v) {
            $initial = getfirstchar(substr($v->name, 0, 3));
            $data[$initial][] = [
                'name' => $v->name,
                'headimgurl' => $v->patternMasterMember->memberRoleInfo->headimgurl ? get_img_url($v->patternMasterMember->memberRoleInfo->headimgurl) : ''
            ];
        }
        ksort($data);
        return $data;
    }
}